home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
59454
/
59454.xpi
/
content
/
util
/
Module.js
< prev
next >
Wrap
Text File
|
2010-01-14
|
3KB
|
104 lines
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
var BartModule;
// make sure we haven't been loaded
if(BartModule && (typeof BartModule != "object" || BartModule.NAME))
{
throw new Error("Namespace 'BartModule' already exists");
}
// create our namespace
BartModule = {};
BartModule.NAME = "BartModule";
BartModule.VERSION = 0.1;
// refer to the global scope
BartModule.globalNamespace = this;
// Module name -> Module map
BartModule.modules = { "Module" : BartModule};
/**
* This function creates and returns a namespace object for the specified name.
* It throws an error if the namespace already exists but the name is not the same, or
* if any of the property components of the namespace exist and are not objects.
*
* Sets a NAME property of the new namespace to its name.
* If the version argument is specified, set the VERSION property of the namespace.
*
* A mapping for the new namespace is added to the BartModule.modules object.
*/
BartModule.createNamespace = function(name, version)
{
if(!name)
{
throw new Error("BartModule.createNamespace(): name required");
}
if(name.charAt(0) == '.'
|| name.charAt(name.length - 1) == '.'
|| name.indexOf("..") != -1)
{
throw new Error("BartModule.createNamespace(): illegal name: " + name);
}
var parts = name.split('.');
var container = BartModule.globalNamespace;
for(var i = 0; i < parts.length; i++)
{
var part = parts[i];
// if there is no property of container with this name, create an empty object
if(!container[part])
{
container[part] = {};
}
else if(typeof container[part] != "object")
{
// if there is already a property, make sure it's an object
var n = parts.splice(0, i).join('.');
throw new Error(n + " already exists and is not an object");
}
container = container[part];
}
var namespace = container;
// it's an error to define a namespace again with a different NAME property.
if(namespace.NAME)
{
if(namespace.NAME != name)
{
throw new Error("Namespace " + name + " exists but with a different NAME property: " + namespace.NAME);
}
}
else
{
namespace.NAME = name;
}
if(version)
{
namespace.VERSION = version;
}
// register this namespace to the module map
BartModule.modules[name] = namespace;
return namespace;
};
/**
* Test whether the module with the specified name has been defined.
* Return true if it's defined and false otherwise.
*/
BartModule.isDefined = function(name)
{
return name in BartModule.modules;
};